<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Build extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->_food = $this->config->item('food');
		$this->_wood = $this->config->item('wood');
		$this->_metal = $this->config->item('metal');
		$this->_petroleum = $this->config->item('petroleum');
		$this->load->model('build/build_model');
	}
    public function index()
	{
	   
        if(!$this->session->userdata('username'))
        {
            redirect(site_url('login'));
            die();
        }
        $data = array();
        //check lai
        $land = 1;
        $uid = $this->session->userdata('uid');
        /*
        * lay thong tin huan luyen quan doi tai manh dat va vi tri nay
        * ------------------------------------------------------------------------------
        */
        
        $this->update_all_res_user($uid, $land);
        $data['build_v2'] = $this->upgrade_build_for_user($uid, $land);
        $rInfo = $this->run_time_res($uid);
        
        $data['rfood'] = $rInfo[0];
        $data['rwood'] = $rInfo[1];
        $data['rmetal'] = $rInfo[2];
        $data['rpetroleum'] = $rInfo[3];
        
        $res = $this->build_model->ww_get_res_user_info_by_field($uid);
        $data['res'] = $res;
        $data['res']['food'] = $res['food'] > $res['maxstore'] ? $res['maxstore'] : $res['food'];
        $data['res']['wood'] = $res['wood'] > $res['maxstore'] ? $res['maxstore'] : $res['wood'];
        $data['res']['metal'] = $res['metal'] > $res['maxstore'] ? $res['maxstore'] : $res['metal'];
        $data['res']['petroleum'] = $res['petroleum'] > $res['maxstore'] ? $res['maxstore'] : $res['petroleum'];
        
        $info_upgrade = $this->build_model->ww_get_user_info_build($uid, $land);

        
        $chk_upgrade = empty($info_upgrade) ? false : true;
        $data['chk_upgrade'] = $chk_upgrade;
        $data['info'] = $info_upgrade;
        
        // header
        
        // content
        $this->load->view('build/build_view', $data);
        
        // footer
    }
    
    function upgrade_build_for_user($uid, $land)
    {
        $upgrade = $this->build_model->ww_get_user_info_build($uid, $land);
        /*
        check 
        */
        $arr_v1 = array();// build complete.
        $arr_v2 = array();// build close complete.
        $build_v2 = array();
        $chk_complete = false;
        foreach($upgrade as $lst)
        {
            $time_of_upgrade = $lst['timestamp'] + $lst['eachtime'];
            $now = time();
            $pastTime = $now - $time_of_upgrade;
            if($pastTime >= 0)
            {
                // complete
                $arr_v1['id'][] = $lst['id'];
                $arr_v1['buildid'][] = $lst['buildid'];
                $chk_complete = true;
            }
            else
            {
                // close complete
                $arr_v2['id'] = $lst['id'];
                $arr_v2['rtime'] = $lst['timestamp'] + $lst['eachtime'] - $now;
                $build_v2[] = $arr_v2;
            }
        }
       
        $strSuccess = '';
		if($chk_complete)
		{
			$lstid = join(",", $arr_v1['id']);
            $i=0;
			foreach($arr_v1['buildid'] as $lstBuild)
			{
				$strSuccess .= 'b'.$lstBuild.'=b'.$lstBuild.'+1,';
				$i++;
			}
            $strSuccess = substr($strSuccess, 0, -1);
            
			$this->build_model->ww_update_build_by_user($uid, $land, $strSuccess, $lstid);
		}
        return $build_v2;
    }
	function run_time_res($uid)
	{
		/*
		chia cho 1 phut
		*/
		$food = 60000/($this->_food/60);
		$wood = 60000/($this->_wood/60);
		$metal = 60000/($this->_metal/60);
		$petroleum = 60000/($this->_petroleum/60);
		
		$result = array($food, $wood, $metal, $petroleum);
		return $result;
	}
    
	function update_all_res_user($uid, $land)
	{
		$data = array();
		$food = $this->_food;
		$wood = $this->_wood;
		$metal = $this->_metal;
		$petroleum = $this->_petroleum;
		$time = time();
		$res = $this->build_model->ww_get_res_user_info_by_field($uid);
		
		$lastupdate = $res['lastupdate'];
		
		$valFloat = (((($time - $lastupdate)/60)/60)/2);
		$food = floor($valFloat*$food);
		$wood = floor($valFloat*$wood);
		$metal = floor($valFloat*$metal);
		$petroleum = floor($valFloat*$petroleum);
        
        $food = $res['maxstore'] < $res['food'] + $food ? $res['maxstore'] - $res['food'] : $food;
        $wood = $res['maxstore'] < $res['wood'] + $wood ? $res['maxstore'] - $res['wood'] : $wood;
        $metal = $res['maxstore'] < $res['metal'] + $metal ? $res['maxstore'] - $res['metal'] : $metal;
        $petroleum = $res['maxstore'] < $res['petroleum'] + $petroleum ? $res['maxstore'] - $res['petroleum'] : $petroleum;
        
		if($food >= 1 || $wood >= 1 || $metal >= 1 || $petroleum >= 1)
		{
			$update_res_by_hour = $this->build_model->ww_update_res_by_hour($uid, $land, $food, $wood, $metal, $petroleum, $time);
		}
	}
 }